<?php

/**
 * 问答页面
 */

namespace frontend\controllers;

use bc\wx\OAuth;
use yii\web\Session;
use bc\util\Util;
use bc\model\shop\ShopUserModel;
use bc\model\shop\WxUserModel;
use bc\model\qa\MoneyModel;
use bc\controller\FrontController;

class QaController extends FrontController
{
	
	private $shareUrl = 'http://wx.zhishimama.com/qa';
	
	private $shareTitle = '一切答题不给奖的都是耍流氓。';
	
	private $shareDesc = '你哭着对我说，连这种题我都答不对……';
	
	public function init()
	{
		
		$session = new Session();
// 		$openid = $_COOKIE['openid'];
		$openid = $session->get('openid');
		$code = $this->get('code', '');
		
		if($openid || $code)
		{
			if($code)
			{
				//获取用户状态
				$appid = $this->wechat()->appid;
				$secret = $this->wechat()->secret;
				$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$secret}&code={$code}&grant_type=authorization_code";
				$rs = Util::httpGet($url);
				$arr = json_decode($rs, true);
				$openid = $arr['openid'];
				$session->set('openid', $arr['openid']);
			}
			
			$shopModel = new ShopUserModel();
			$user = $shopModel->getRow(['wxid' => $openid]);
			
			$wxUserModel = new WxUserModel();
			$wxUser = $wxUserModel->getRow(['wxid' => $openid]);
			
			
			if($user)
			{
				$user['subscribe'] = $wxUser['subscribe'];
				$session->set('wxuser', $user);
				
				$moneyModel = new MoneyModel();
				$row = $moneyModel->getRow(['openid' => $openid]);
				if(!$row)
				{
					//create row
					$data['openid'] = $openid;
					$data['create_time'] = time();
					$moneyModel->add($data);
				}
				
			}
		}
		else 
		{
			$oauth = new OAuth();
			$redirectUri = $this->getFullUrl();
			$oauth->authorize($redirectUri, OAuth::SNSAPI_BASE);
		}
		
	}
	
	
	/**
	 * 首页
	 */
	public function actionIndex()
	{
		// 生成js sdk签名
		$jsConfig = $this->configJsApi ();
		$appid = $this->wechat ()->appid;
		$secret = $this->wechat ()->secret;
		$view ['share_url'] = $this->shareUrl;
		$view ['share_desc'] = $this->shareDesc;
		$view ['share_title'] = $this->shareTitle;
		$view ['js_config'] = $jsConfig;
		
		return $this->render('index', $view);
	}
	
	/**
	 * 结果页 
	 */
	public function actionResult()
	{
		// 生成js sdk签名
		$jsConfig = $this->configJsApi ();
		$appid = $this->wechat ()->appid;
		$secret = $this->wechat ()->secret;
		$view ['share_url'] = $this->shareUrl;
		$view ['share_desc'] = $this->shareDesc;
		$view ['share_title'] = $this->shareTitle;
		$view ['js_config'] = $jsConfig;
		
		$session = new Session();
		$user = $session->get('wxuser');
		$view ['user'] = $user;
		
		return $this->render('result', $view);
	}
	
	
	/**
	 * 优惠券
	 */
	public function actionCoupon()
	{
		$session = new Session();
		$user = $session->get('wxuser');
		
		$moneyModel = new MoneyModel();
		$row = $moneyModel->getRow(['openid' => $user['wxid']]);
		
		$view['row'] = $row;
		
		// 生成js sdk签名
		$jsConfig = $this->configJsApi ();
		$appid = $this->wechat ()->appid;
		$secret = $this->wechat ()->secret;
		$view ['share_url'] = $this->shareUrl;
		$view ['share_desc'] = $this->shareDesc;
		$view ['share_title'] = $this->shareTitle;
		$view ['js_config'] = $jsConfig;
		
		return $this->render('coupon', $view);
	}
	
	
	/**
	 * 领取默认优惠券
	 */
	public function actionDocoupon()
	{
		$session = new Session();
		$user = $session->get('wxuser');
		
		$moneyModel = new MoneyModel();
		$row = $moneyModel->getRow(['openid' => $user['wxid']]);
			
		if($user['subscribe'] == 1)
		{
			if($row['default'] == 0)
			{
				//发放默认红包 20元和30元 红包id为18 19
				$this->sendLuckMoney($user['user_id'], 18);
				$this->sendLuckMoney($user['user_id'], 19);
				
				$data['default'] = 1;
				$data['default_time'] = time();
				$moneyModel->modify($data, $row['id']);
			}
		}
		
		$this->redirect('/qa/coupon');
		
	}
	
	
	/**
	 * ajax请求领取优惠券
	 */
	public function actionAjaxcoupon()
	{
		$res['error'] = 1;
		
		if($this->getRequest()->isAjax && $this->getRequest()->isPost)
		{
			$res['error'] = 1;
			
			//1:默认领券 2：分享领券
			$type = $this->post('type');
			$session = new Session();
			$user = $session->get('wxuser');
			
			$moneyModel = new MoneyModel();
			$row = $moneyModel->getRow(['openid' => $user['wxid']]);
				
			if($type == 1 && $row['default'] == 0 && $user['subscribe'] == 1)
			{
				//发放默认红包 20元和30元 红包id为18 19
				$this->sendLuckMoney($user['user_id'], 18);
				$this->sendLuckMoney($user['user_id'], 19);
				
				$data['default'] = 1;
				$data['default_time'] = time();
				$moneyModel->modify($data, $row['id']);
				$res['error'] = 0;
			}
			else if($type == 2 && $row['share'] == 0 && $user['subscribe'] == 1)
			{
				
				//发放分享红包 20、30、50 红包id为20, 21, 22
				$this->sendLuckMoney($user['user_id'], 20);
				$this->sendLuckMoney($user['user_id'], 21);
				$this->sendLuckMoney($user['user_id'], 22);
				
				$data['share'] = 1;
				$data['share_time'] = time();
				$moneyModel->modify($data, $row['id']);
				$res['error'] = 0;
			}
			else if($user['subscribe'] == 0)
			{
				//未关注
				$res['error'] = 3;
			}
			
		}
		
		echo json_encode($res);
		exit;
	}
	
	
	/**
	 * 发送红包的动作
	 */
	private function sendLuckMoney($userId, $luckMoneyId)
	{
		$url = 'http://wx.zhishimama.com/shop/api.php?';
		$url .= 'act=send_redpacket_by_user&api_version=1.0&return_data=json&type_id='.$luckMoneyId;
		$user[] = $userId;
		$user = json_encode($user);
		$url .= '&userlist='.$user;
		$rs = Util::httpGet($url);
		return $rs;
	}
}



